package net.huashitong.interview.dao;

import com.sirdc.modules.core.dao.GenericQuery;
import com.sirdc.modules.core.dao.StringDao;
import com.sirdc.modules.core.filter.BaseFilter;
import com.sirdc.modules.utils.ObjectUtils;
import net.huashitong.interview.entity.InterviewRegister;
import net.huashitong.interview.filter.InterviewRegisterFilter;
import org.hibernate.SQLQuery;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class RegisterDao extends StringDao<InterviewRegister> {

    public List<InterviewRegister> queryRegister(InterviewRegisterFilter filter){

        StringBuffer sql = new StringBuffer("select " +
                "t.sys_id as sysId," +
                "t.business_problem as businessProblem," +
                "t.contact_business as contactBusiness," +
                "t.interview_count_this_month as interviewCountThisMonth," +
                "t.fill_date as fillDate," +
                "t.fill_unit_id as fillUnitId," +
                "t.help_experience as helpExperience," +
                "t.solve_state as solveState," +
                "t.user_name as userName " +
                "from t_gis_interview_register t where 1=1");

        if(ObjectUtils.isNotBlank(filter.getFillUnitId())){
            sql.append(" and t.fill_unit_id ='"+filter.getFillUnitId()+"'");
        }
        if(ObjectUtils.isNotBlank(filter.getFillDate())){
            sql.append(" and t.fill_date like '"+filter.getFillDate()+"%'");
        }

        //模糊查询
        if(ObjectUtils.isNotBlank(filter.getSearchWord())){
            sql.append(" and t.user_name like '%"+filter.getSearchWord()+"%' " +
                    "or t.contact_business like '%"+filter.getSearchWord()+"%'");
        }

        SQLQuery query = getSession().createSQLQuery(sql.toString());
        query.setResultTransformer(Transformers.aliasToBean(InterviewRegister.class));
        List<InterviewRegister> list  = (List<InterviewRegister>)query.list();
        return list;
    }

    public String getNewFillDate(){

        GenericQuery query = createQuery("obj");
        query.setOrder("fillDate",BaseFilter.DESC);
        InterviewRegister register = query.firstResult();
//
//        StringBuffer sql = new StringBuffer("select " +
//                "t.fill_date as fillDate," +
//                "from t_gis_interview_register t GROUP BY fill_date ORDER BY fill_date desc");
//        SQLQuery query = getSession().createSQLQuery(sql.toString());
//        query.setResultTransformer(Transformers.aliasToBean(String.class));
//        List<String> strList = (List<String>)query.list();
        return register.getFillDate();
    }


}
